﻿#region MIT
/*
Engine Of Evermore (http://code.google.com/p/engineofevermore/)
Copyright (c) 2008, 2009 Michael Woerister

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using NUnit.Framework;
using EngineOfEvermore.Scene;
using EngineOfEvermore.Testing;
using EngineOfEvermore.Scene.Visual;
using EngineOfEvermore.Common;
using EngineOfEvermore.Imaging;
using System.Diagnostics;

namespace EngineOfEvermore.Editor.SceneTools
{
    interface ISelectionRectManager
    {
        bool SelectionRectVisible { get; set; }
        void SetSelectionRect( Rectangle rectInScreenCoordinates );
    }

    [SerializerIgnore]
    class SelectionRectComponent : SceneObjectComponent
    {
        class InternalDrawable : Drawable 
        {
            public void SetAABB( RectangleF aabb )
            {
                _aabb = aabb;
            }

            public override void Draw( RectangleF visibleArea, IRenderer r )
            {
                r.DrawRect( _aabb );
            }
        }

        InternalDrawable _drawable = new InternalDrawable();

        public RectangleF SelectionRect
        {
            get { return _drawable.AABB; }
            set 
            {
                _drawable.SetAABB( value );
            }
        }

        protected override void OnAddToScene( SceneGraph scene )
        {
            base.OnAddToScene( scene );
            scene.GetSubSystem<VisualSubSystem>().RegisterDrawable( _drawable );
        }

        protected override void OnRemoveFromScene( SceneGraph scene )
        {
            scene.GetSubSystem<VisualSubSystem>().UnregisterDrawable( _drawable );
            base.OnRemoveFromScene( scene );
        }

        public override SceneObjectComponent Clone()
        {
            return new SelectionRectComponent();
        }
    }

    class SelectionRectManager : ISelectionRectManager
    {
        bool _rectVisible;
        readonly SceneGraph _sceneGraph;
        readonly SceneObject _sceneObject;
        readonly Camera _camera;

        public SelectionRectManager( SceneGraph sceneGraph, Camera camera )
        {
            Debug.Assert( sceneGraph != null );
            Debug.Assert( camera != null );

            _sceneGraph = sceneGraph;
            _camera = camera;
            _sceneObject = new SceneObject();
            _sceneObject.AddComponent( new SelectionRectComponent() );
        }

        public bool SelectionRectVisible
        {
            get { return _rectVisible; }
            set
            {
                if ( _rectVisible == value )
                    return;

                _rectVisible = value;

                if ( _rectVisible )
                {
                    _sceneGraph.AddSceneObject( _sceneObject );
                }
                else
                {
                    _sceneGraph.RemoveSceneObject( _sceneObject );
                }
            }
        }

        public void SetSelectionRect( Rectangle rectInScreenCoordinates )
        {
            var worldSelectionRect = _camera.ToWorld( rectInScreenCoordinates );
            _sceneObject.GetComponent<SelectionRectComponent>().SelectionRect = worldSelectionRect;
        }

        #if ENGINE_OF_EVERMORE_ENABLE_UNIT_TESTS
        [TestFixture]
        public class TestFixture
        {
            [Test]
            public void TestRectNotVisibleByDefault()
            {
                SelectionRectManager srm = new SelectionRectManager( 
                    TestSceneFactory.CreateEmptyInitializedTestScene(),
                    new Camera()
                    );

                Assert.That( srm.SelectionRectVisible, Is.False );
            }

            [Test]
            public void TestRectSceneObjectContainedInSceneGraphWhenVisible()
            {
                var sceneGraph = TestSceneFactory.CreateEmptyInitializedTestScene();
                sceneGraph.UpdateOnce();
                SelectionRectManager srm = new SelectionRectManager( sceneGraph, new Camera() );

                srm.SelectionRectVisible = true;

                sceneGraph.UpdateNTimes( 2 );

                Assert.That( sceneGraph.SceneObjects.Count(
                    so => so.GetComponent<SelectionRectComponent>() != null ), Is.EqualTo( 1 ) );
            }

            [Test]
            public void TestRectSizeMatches()
            {
                var camera = new Camera();
                camera.ViewportSize = new Size( 800, 600 );
                var sceneGraph = TestSceneFactory.CreateEmptyInitializedTestScene();
                sceneGraph.UpdateOnce();
                
                SelectionRectManager srm = new SelectionRectManager( sceneGraph, camera );
                var selectionRect = new Rectangle( 10, 20, 30, 40 );

                srm.SelectionRectVisible = true;
                srm.SetSelectionRect( selectionRect );

                sceneGraph.UpdateNTimes( 2 );

                Assert.That( sceneGraph.SceneObjects.Count(
                    so => so.GetComponent<SelectionRectComponent>() != null ), Is.EqualTo( 1 ) );

                var rectComponent = sceneGraph.SceneObjects
                    .Where( so => so.GetComponent<SelectionRectComponent>() != null )
                    .Select( so => so.GetComponent<SelectionRectComponent>() )
                    .Single();

                Assert.That( rectComponent.SelectionRect, 
                    Is.EqualTo( camera.ToWorld( selectionRect ) ) );
            }

            [Test]
            public void TestIfSettingNotVisibleRemovesSceneObject()
            { 
                var sceneGraph = TestSceneFactory.CreateEmptyInitializedTestScene();
                SelectionRectManager srm = new SelectionRectManager( sceneGraph, new Camera() );

                srm.SelectionRectVisible = true;
                sceneGraph.UpdateNTimes( 2 );
                srm.SelectionRectVisible = false;

                Assert.That( sceneGraph.SceneObjects.Count(
                    so => so.GetComponent<SelectionRectComponent>() != null ), Is.EqualTo( 0 ) );
            }
        }
        #endif
    }
}
